<template>
  <div class="practice">
    <div class="progress-bar">
      <div class="progress">
        题目 {{ currentIndex + 1 }}/{{ batchSize }} | 得分: {{ score }}
      </div>
    </div>
    
    <div v-if="currentQuestion" class="question-card">
      <div class="question-content">
        <h3>题目：</h3>
        <p>{{ currentQuestion.content }}</p>
        
        <div v-if="currentQuestion.imageUrl" class="image-container">
          <img :src="currentQuestion.imageUrl" alt="题目图片" />
        </div>
        
        <div v-if="currentQuestion.hint" class="hint-button">
          <button @click="showHint = !showHint">
            {{ showHint ? '隐藏提示' : '显示提示' }}
          </button>
          <div v-if="showHint" class="hint">
            {{ currentQuestion.hint }}
          </div>
        </div>

        <div class="answer-input" v-if="!submitted">
          <textarea 
            v-model="userAnswer" 
            rows="4" 
            placeholder="请输入你的答案..."
          ></textarea>
          <button @click="submitAnswer" :disabled="!userAnswer">提交答案</button>
        </div>

        <div v-else class="result">
          <div class="your-answer">
            <h4>你的答案：</h4>
            <p>{{ userAnswer }}</p>
          </div>

          <div class="correct-answer">
            <h4>参考答案：</h4>
            <p>{{ currentQuestion.answer }}</p>
          </div>

          <div class="self-score">
            <p>对比参考答案，为自己打分：</p>
            <div class="score-buttons">
              <button 
                v-for="score in [0, 2, 4, 6, 8, 10]" 
                :key="score"
                @click="gradeAnswer(score)"
                :class="{ active: selfScore === score }"
              >
                {{ score }}分
              </button>
            </div>
          </div>
        </div>
      </div>

      <div class="actions" v-if="submitted">
        <button 
          @click="nextQuestion" 
          class="next-btn"
          :disabled="selfScore === null"
        >
          {{ isLastQuestion ? '查看总结' : '下一题' }}
        </button>
      </div>
    </div>

    <div v-else-if="showSummary" class="summary">
      <h2>练习总结</h2>
      <div class="score-summary">
        <p>总分：{{ score }}/{{ batchSize * 10 }}</p>
        <p>正确率：{{ Math.round(score / (batchSize * 10) * 100) }}%</p>
      </div>
      <button @click="startNewBatch" class="start-new">开始新一轮</button>
    </div>

    <div v-else class="loading">
      加载题目中...
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useQuestionStore } from '../stores/question'

const questionStore = useQuestionStore()
const batchSize = 10 // 每组10道题
const questions = ref([])
const currentIndex = ref(0)
const score = ref(0)
const userAnswer = ref('')
const submitted = ref(false)
const showHint = ref(false)
const selfScore = ref(null)
const showSummary = ref(false)

const currentQuestion = computed(() => 
  questions.value[currentIndex.value] || null
)

const isLastQuestion = computed(() => 
  currentIndex.value === batchSize - 1
)

async function loadQuestions() {
  try {
    // 获取10道题
    const loadedQuestions = []
    for (let i = 0; i < batchSize; i++) {
      const question = await questionStore.getRandomQuestion()
      loadedQuestions.push(question)
    }
    questions.value = loadedQuestions
    resetState()
  } catch (error) {
    console.error('获取题目失败:', error)
  }
}

function resetState() {
  currentIndex.value = 0
  score.value = 0
  showSummary.value = false
  resetQuestionState()
}

function resetQuestionState() {
  userAnswer.value = ''
  submitted.value = false
  showHint.value = false
  selfScore.value = null
}

function submitAnswer() {
  submitted.value = true
}

function gradeAnswer(points) {
  selfScore.value = points
  score.value += points
}

function nextQuestion() {
  if (isLastQuestion.value) {
    showSummary.value = true
  } else {
    currentIndex.value++
    resetQuestionState()
  }
}

function startNewBatch() {
  loadQuestions()
}

// 初始加载
loadQuestions()
</script>

<style scoped>
.practice {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.progress-bar {
  background: #f5f5f5;
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 20px;
}

.question-card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
}

.image-container {
  margin: 15px 0;
  text-align: center;
}

.image-container img {
  max-width: 100%;
  max-height: 300px;
  border-radius: 4px;
}

.hint-button {
  margin: 15px 0;
}

.hint {
  margin-top: 10px;
  padding: 10px;
  background: #f8f8f8;
  border-left: 4px solid #42b983;
}

.answer-input {
  margin: 20px 0;
}

textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin-bottom: 10px;
}

button {
  padding: 8px 16px;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:disabled {
  background: #a8d5c2;
  cursor: not-allowed;
}

.result {
  margin-top: 20px;
  padding: 15px;
  background: #f5f5f5;
  border-radius: 4px;
}

.your-answer, .correct-answer {
  margin-bottom: 15px;
}

.score-buttons {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.score-buttons button {
  flex: 1;
  background: #666;
}

.score-buttons button.active {
  background: #42b983;
}

.next-btn {
  margin-top: 20px;
  width: 100%;
}

.summary {
  text-align: center;
  padding: 30px;
  background: #f5f5f5;
  border-radius: 8px;
}

.score-summary {
  margin: 20px 0;
  font-size: 1.2em;
}

.start-new {
  padding: 12px 24px;
  font-size: 1.1em;
}

.loading {
  text-align: center;
  padding: 40px;
  color: #666;
}
</style> 